package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.KeywordVo;
import com.cskaoyan.service.KeywordService;
import com.cskaoyan.service.KeywordServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;

@WebServlet("/admin/keyword/*")
public class AdminKeywordServlet extends WdBaseServlet {
    KeywordService keywordService = new KeywordServiceImpl();

    private BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        // 1. 请求参数的接收
        String pageStr = request.getParameter("page");
        Integer page;
        if (pageStr != null && pageStr != "") {
            page = Integer.parseInt(pageStr);
        } else {
            page = null;
        }

        String limitStr = request.getParameter("limit");
        Integer limit;
        if (limitStr != null && limitStr != "") {
            limit = Integer.parseInt(limitStr);
        } else {
            limit = null;
        }
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        String keyword = request.getParameter("keyword");
        String url = request.getParameter("url");

        // 2. Service封装对象
        BaseRespVo baseRespVo = keywordService.list(page, limit, keyword, url, sort, order);

        // 3. 将对象转为Json字符串，通过response进行响应
        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {
        // 1. 请求参数的接收
        String resJstr = request.getReader().readLine();
        ObjectMapper objectMapper = new ObjectMapper();
        MarketKeyword marketKeyword = objectMapper.readValue(resJstr, MarketKeyword.class);

        // 2. Service封装对象
        BaseRespVo baseRespVo = keywordService.create(marketKeyword);

        // 3. 将对象转为Json字符串，通过response进行响应
        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        // 1. 请求参数的接收
        String resJstr = request.getReader().readLine();
        ObjectMapper objectMapper = new ObjectMapper();
        KeywordVo keywordVo = objectMapper.readValue(resJstr, KeywordVo.class);

        // 2. Service封装对象
        BaseRespVo baseRespVo = keywordService.update(keywordVo);

        // 3. 将对象转为Json字符串，通过response进行响应
        return baseRespVo;
        // return null;
    }

    @SneakyThrows
    private BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        // 1. 请求参数的接收
        String resJstr = request.getReader().readLine();
        ObjectMapper objectMapper = new ObjectMapper();
        KeywordVo keywordVo = objectMapper.readValue(resJstr, KeywordVo.class);

        // 2. Service封装对象
        BaseRespVo baseRespVo = keywordService.delete(keywordVo);

        // 3. 将对象转为Json字符串，通过response进行响应
        return baseRespVo;

    }
}
